﻿namespace BTS.COMMON
{
    using System;
    using System.IO;
    using System.Text;
    using System.Drawing;

    public abstract class WatermarkPainter : IDisposable
    {
        private string m_ImageToBePainted;
        private Image m_RawImage;
        private bool m_ReplaceRawImage;
        private string m_TempFileSuffix;
        private string m_TempImagePath;
        private int m_WatermarkAlpha;

        protected WatermarkPainter(string rawImagePath, int alpha) : this(rawImagePath, alpha, true)
        {
        }

        protected WatermarkPainter(string rawImagePath, int alpha, bool replaceRawImage)
        {
            this.m_TempFileSuffix = ".qz.watermark.temp";
            this.ImageToBePainted = rawImagePath;
            this.WatermarkAlpha = alpha;
            this.ReplaceRawImage = replaceRawImage;
        }

        protected abstract bool AddWatermark(Graphics graphics, Bitmap newImage);
        public virtual void Dispose()
        {
        }

        public void PaintWaterMark()
        {
            if (!string.IsNullOrEmpty(this.ImageToBePainted))
            {
                bool flag;
                if (!File.Exists(this.ImageToBePainted))
                {
                    throw new FileNotFoundException(string.Format("file {0} not found.", this.ImageToBePainted));
                }
                if (this.m_RawImage == null)
                {
                    this.m_RawImage = Image.FromFile(this.ImageToBePainted);
                }
                using (Bitmap bitmap = new Bitmap(this.m_RawImage.Width, this.m_RawImage.Height))
                {
                    using (Graphics graphics = Graphics.FromImage(bitmap))
                    {
                        graphics.DrawImage(this.m_RawImage, 0, 0, this.m_RawImage.Width, this.m_RawImage.Height);
                        flag = this.AddWatermark(graphics, bitmap);
                    }
                    if (flag)
                    {
                        bitmap.Save(this.TempImagePath);
                    }
                    if (this.m_RawImage != null)
                    {
                        this.m_RawImage.Dispose();
                    }
                }
                if (flag && this.ReplaceRawImage)
                {
                    if (File.Exists(this.ImageToBePainted))
                    {
                        File.Delete(this.ImageToBePainted);
                    }
                    FileInfo info = new FileInfo(this.TempImagePath);
                    if (info.Exists)
                    {
                        info.MoveTo(this.ImageToBePainted);
                    }
                }
            }
        }

        public string ImageToBePainted
        {
            get
            {
                return this.m_ImageToBePainted;
            }
            set
            {
                this.m_ImageToBePainted = value;
            }
        }

        public bool ReplaceRawImage
        {
            get
            {
                return this.m_ReplaceRawImage;
            }
            set
            {
                this.m_ReplaceRawImage = value;
            }
        }

        protected string TempImagePath
        {
            get
            {
                if (string.IsNullOrEmpty(this.m_TempImagePath))
                {
                    StringBuilder builder = new StringBuilder(this.ImageToBePainted);
                    builder.Append(this.m_TempFileSuffix);
                    this.m_TempImagePath = builder.ToString();
                }
                return this.m_TempImagePath;
            }
            set
            {
                this.m_TempImagePath = value;
            }
        }

        public int WatermarkAlpha
        {
            get
            {
                if (this.m_WatermarkAlpha > 0xff)
                {
                    this.m_WatermarkAlpha = 0xff;
                }
                else if (this.m_WatermarkAlpha < 0)
                {
                    this.m_WatermarkAlpha = 0;
                }
                return this.m_WatermarkAlpha;
            }
            set
            {
                this.m_WatermarkAlpha = value;
            }
        }
    }
}

